﻿using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using ConfExpert.Utility.Data;
using System.Collections.Generic;

public class DataAccess
{
    private readonly static string cnnStrCe = System.Configuration.ConfigurationManager.ConnectionStrings["ce"].ConnectionString;
    private readonly static DataManager dataManager = new DataManager(cnnStrCe);


    public static void web2SetSelectedProducts(List<int> productIds, List<int> rollIds, decimal qtyRequest, string tab)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ProductId", typeof(int)));
        foreach (var pId in productIds)
        {
            dt.Rows.Add(pId);
        }
        dt.Namespace = "SelectedProducts";
        dt.TableName = "SelectedProduct";
        string xml = Serialize(dt);



        DataTable dtRoll = new DataTable();
        dtRoll.Columns.Add(new DataColumn("RollId", typeof(int)));
        foreach (var rollId in rollIds)
        {
            dtRoll.Rows.Add(rollId);
        }
        dtRoll.Namespace = "SelectedRolls";
        dtRoll.TableName = "SelectedRoll";
        string xmlRoll = Serialize(dtRoll);


        dataManager.ExecuteNonQuery("web2SetSelectedProducts", xml, xmlRoll, qtyRequest, Sessions.CurrentMember.CustomerId, tab);
    }


    public static DataSet web2GetStok(string DetailLevel, string Tab)
    {
        return dataManager.ExecuteDataset("web2GetStok", Sessions.CurrentMember.CustomerId, Tab, DetailLevel);
    }

    public static DataTable web2GetDU4Missing()
    {
        return dataManager.ExecuteDataTableByQuery("Select * from Du4Missing");
    }





    public static DataTable web2GetReservation()
    {
        return dataManager.ExecuteDataTable("web2GetReservation", Sessions.CurrentMember.CustomerId);
    }

  

 public static DataSet web2GetDU4Orders()
    {
        return dataManager.ExecuteDataset("web2GetDU4Orders");
    }



    public static DataSet web2InsRezerve(int BrandId, int SezonId, int RezerveSuresi, string Notes, string OrderRef, string ReservedBy, string email)
    {
        DataTable rezerve = new DataTable();
        rezerve.Columns.Add("BrandId", typeof(int));
        rezerve.Columns.Add("SezonId", typeof(int));
        rezerve.Columns.Add("SODetay", typeof(string));
        rezerve.Columns.Add("Notes", typeof(string));
        rezerve.Columns.Add("RezerveSuresi", typeof(int));
        rezerve.Columns.Add("ReservedBy", typeof(string));
        rezerve.Columns.Add("email", typeof(string));

        DataRow dr = rezerve.NewRow();
        dr["BrandId"] = BrandId;
        dr["SezonId"] = SezonId;
        dr["RezerveSuresi"] = RezerveSuresi;
        dr["Notes"] = Notes;
        dr["SODetay"] = OrderRef;
        dr["ReservedBy"] = ReservedBy;
        dr["email"] = email;

        rezerve.Rows.Add(dr);
        rezerve.TableName = "Rezerve";
        rezerve.Namespace = "MyData";

        string xmlRezerve = DataAccess.Serialize(rezerve);
        //string xmlRezerveLines = DataAccess.Serialize(Sessions.SelectedProducts);
        //DataSet ds =dataManager.ExecuteDataset("webInsRezerve", xmlRezerve, xmlRezerveLines);
        DataSet ds = dataManager.ExecuteDataset("web2InsRezerve", xmlRezerve, Sessions.CurrentMember.CustomerId);
        return ds;
    }



    public static DataTable web2AvailableRolls(List<int> productIds, string tab)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ProductId", typeof(int)));
        foreach (var pId in productIds)
        {
            dt.Rows.Add(pId);
        }
        dt.Namespace = "SelectedProducts";
        dt.TableName = "SelectedProduct";
        string xml = Serialize(dt);
        return dataManager.ExecuteDataTable("web2AvailableRolls", xml, Sessions.CurrentMember.CustomerId, tab);
    }



    public static DataTable web2AvailableRolls(int productId, string tab)
    {
        List<int> pIds = new List<int>();
        pIds.Add(productId);
        return web2AvailableRolls(pIds, tab);
    }

    public static DataTable GetTinyEyeProducts(string productIds,string tab)
    {
        return dataManager.ExecuteDataTable("GetTinyEyeProductsWeb", productIds,Sessions.CurrentMember.CustomerId,tab);
    }
    public static DataTable web2GetBlockedQtyDetail(int productId)
    {
        return dataManager.ExecuteDataTable("web2GetBlockedQtyDetail", productId);
    }



    public static DataTable Seasons()
    {
        return dataManager.ExecuteDataTableByQuery ("SELECT SezonId,Name FROM Sezon");
    }

    

    public static DataTable Brands()
    {
        return dataManager.ExecuteDataTableByQuery("SELECT BrandId,Name FROM Brand WHERE CustomerId=" + Sessions.CurrentMember.CustomerId);        
    }

    public static bool Authenticate(string UserName, string Password)
    {
        if (UserName.ToLower() == "---")
        {
            Sessions.CurrentMember.UserName = UserName;
            Sessions.CurrentMember.Name = "7 CAMICIE";
            Sessions.CurrentMember.CustomerId = 20;
            Sessions.CurrentMember.IsDebugSession = true;
            
            return true;
        }
        else
        {
            DataTable dt = dataManager.ExecuteDataTable("web2GetUser", UserName, Password);
            if (dt.Rows.Count > 0)
            {
                Sessions.CurrentMember.UserName = UserName;
                Sessions.CurrentMember.Name = dt.Rows[0]["Name"].ToString();
                Sessions.CurrentMember.Culture = dt.Rows[0]["Culture"].ToString();
                Sessions.CurrentMember.CustomerId = (int)dt.Rows[0]["CustomerId"];

                Sessions.CurrentMember.IsDebugSession = (Password == "***");
                return true;
            }
            else
            {
                return false;
            }
        }       
    }

    public class Product
    {
        string description = string.Empty;
        public string ProductName { get; set; }
        public int ProductId { get; set; }

        public Product(DataRow dr)
        {    
            ProductName = (string)dr["Article"];
            ProductId = (int)dr["ProductId"];

            if (dr["Composition"] != DBNull.Value)
                this.Description = (string)dr["Composition"];
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }
    }

    

    public static string Serialize(DataTable table)
    {
        string rootElement = table.Namespace;
        table.Namespace = string.Empty;
        new XmlSerializerNamespaces().Add(string.Empty, string.Empty);
        StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
        XmlTextWriter writer = new ConfExpertXmlWriter(w);        
        table.WriteXml(writer, XmlWriteMode.IgnoreSchema);
        string result = w.ToString().Replace("DocumentElement", rootElement);
        table.Namespace = rootElement;
        return result;
    }

      public class ConfExpertXmlWriter : System.Xml.XmlTextWriter
    {
        public ConfExpertXmlWriter(System.IO.TextWriter w)
            : base(w)
        {
            //Formatting = System.Xml.Formatting.Indented;
        }

        public override void WriteStartDocument()
        {
            //to suppress
        }
    }

    public static bool ChangePassword(int CustomerId, string CureentPass, string NewPass)
    {
        return (dataManager.ExecuteNonQuery("ChangePass", CustomerId, CureentPass, NewPass) == 1);        
    }



    public static void web2SetSelectionForce(DataTable dtForce, string Tab)
    {
        dtForce.Namespace = "SelectedProducts";
        dtForce.TableName = "SelectedProduct";
        string xml = Serialize(dtForce);
        dataManager.ExecuteDataTable("web2SetSelectionForce", xml, Sessions.CurrentMember.CustomerId, Tab);        
    }

    

    public static DataTable web2GetStokFields(string tab)
    {
        DataTable dt = dataManager.ExecuteDataTable("web2GetStokFields", Sessions.CurrentMember.CustomerId,tab);
        return dt;
    }

    public static DataTable web2GetDU4MissingDetail(int Du4MissingId)
    {
        DataTable dt = dataManager.ExecuteDataTable("web2GetDU4MissingDetail", Du4MissingId);
        return dt;
    }
}